package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

//    新增分类
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.add(categoryDTO);
        return Result.success();
    }

//    分页查询
    @GetMapping("/page")
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

//    启用禁用分类
    @PostMapping("/status/{status}")
    public Result setStatus(@PathVariable Integer status,Long id) {
        categoryService.setStatus(status,id);
        return Result.success();
    }

    //根据类型查询分类
    @GetMapping("/list")
    public Result selectByType( Integer type) {
        List<Category> list = categoryService.selectByType(type);
        return Result.success(list);
    }

//    修改分类信息
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateCategory(categoryDTO);
        return Result.success();

    }

//    删除分类
    @DeleteMapping
    public Result deleteCategory(Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

}
